#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <omp.h>
#include "preprocessor.h"
#include "row.h"


int main(int argc, char* args[]) {

  FILE *pFile;
  node *words = NULL;
  node *words2 = NULL;
  node *words5 = NULL;
  node *words10 = NULL;
  node *words15 = NULL;
  node *words20 = NULL;
  node *words25 = NULL;


  int i, j, counter_wordTotal = 0, counter_wordParcial = 0, words_size[46] = {0};

  words = preProcessor(&counter_wordTotal,words_size,words5, words10, words15, words20, words25);
  
  for(i=0;i<46;i++)
    printf("%d palavras de tamanho %d, %d porcento\n",words_size[i],i,(words_size[i]*100)/counter_wordTotal);


  printf("\ndepois %d\n", counter_wordTotal);
  pFile = fopen("res_word_3.txt", "w");
  int n, w;
  char c;
  int th_id, flag = 0, porcent = 0;

  printf("nó %s, numero de blocos %d\n", words->word,words->blocks);
  printf("nó %s, numero de blocos %d\n", words->left->word,words->left->blocks);


#pragma omp parallel private(th_id,i,j)   
  {
    int r;
    th_id = omp_get_thread_num();
    srand(time(NULL) * th_id);
    n = rand();
    printf("rand = %d\n", n);

    if (th_id % 2 == 0) { // versao 2
      char chars[28] = {'e', 't', 'a', 'o', 'i', 'n', 's', 'h', 'r', 'd',
        'l', 'c', 'u', 'm', 'w', 'f', 'g', 'y', 'p', 'b', 'v', 'k', 'j',
        'x', 'q', 'z', '{'};
      char ch;
      int val;
      int loop = 0;
      int size_word;
      char s[50] = {0};
      

      for (j = 0; 1; j++) {
        if (flag > 1000000) {
          srand(time(NULL) * th_id);
          r = rand() % 27;
          for (w = 0; w < 27; w++)
            chars[w] = ((chars[w] - 'a') + r) % 27 + 'a';
          //           printf("1: mudou seed na thread %d\n novo alfabeto %s \n",th_id,chars);
          flag = 0;
        }
        val = j;
        
      
        
        for (i = 0; i < 100; i++) {
          if(flag%2==1)
            i = strlen(s);
          
          flag++;
          

          if (findN(s, words, i) == NULL)
            i = 99;

          ch = chars[val % 27];
          s[i] = ch;
          s[i+1] = '\0';
          val = val / 27;

          if (find(s, words) != NULL) {
            if (find(s, words2) == NULL) {
              flag = 0;
              words2 = insert(s, words2);
              fprintf(pFile, "%s\n", s);
              counter_wordParcial++;
              size_word = strlen(s);
              
                     
              
              if (porcent != (counter_wordParcial * 100) / counter_wordTotal) {
                
                porcent = (counter_wordParcial * 100) / counter_wordTotal;
                printf("v2: total %d encontrados %d porcentagem %d th_id %d j %d i %d\n", counter_wordTotal, counter_wordParcial,
                        (counter_wordParcial * 100) / counter_wordTotal, th_id, j, i);
               }
                 
              }
            }
            if (counter_wordParcial == counter_wordTotal)
              exit(0);
          }
        }
      }
     else { // versao 1
      char chars[28] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        'x', 'y', 'z', '{'};
      int r;
      th_id = omp_get_thread_num();
      int size_word;

      srand(time(NULL) * th_id);
      chars[27] = '\0';
      while (1) {
      char s[50] = {0};

        if (flag > 100000) {
          srand(time(NULL) * th_id);
          //             printf("2: mudou seed na thread %d\n novo alfabeto %s \n",th_id,chars);
          flag = 0;
          r = rand() % 27;
          for (w = 0; w < 27; w++)
            chars[w] = ((chars[w] - 'a') + r) % 27 + 'a';

        }

        for (i = 1; i <= 100; i++) {
          flag++;
          
          for (j = 1; j <= i; j++) {
            
            n = rand() % 27;
            c = chars[n];
            s[j - 1] = c;

            if (findN(s, words, j) == NULL)
              j = i;

#pragma omp critical
            if (find(s, words) != NULL) {
              if (find(s, words2) == NULL) {
                flag = 0;
                words2 = insert(s, words2);

                fprintf(pFile, "%s\n", s);
                counter_wordParcial++;
                
               
                if (porcent != (counter_wordParcial * 100) / counter_wordTotal) {
                  porcent = (counter_wordParcial * 100) / counter_wordTotal;
                  printf("v1: total %d encontrados %d porcentagem %d th_id %d j %d i %d\n", counter_wordTotal, counter_wordParcial,
                          (counter_wordParcial * 100) / counter_wordTotal, th_id, j, i);
                }
              }
              if (counter_wordParcial == counter_wordTotal)
                exit(0);
            }
          }
        }
      }
    }
  }
  fclose(pFile);







  return 0;
}


